﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ecreate.Web.popedom.userManage
{
    public partial class userCrud : System.Web.UI.Page
    {
        Ecreate.Popedom.Service.IUserService userService = new Ecreate.Popedom.Service.SpringImpl.UserServiceImpl();
        Ecreate.Popedom.Service.IGroupService groupService = new Ecreate.Popedom.Service.SpringImpl.GroupServiceImpl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {

                this.ddl_group.DataSource = groupService.getAll();
                this.ddl_group.DataTextField = "Groupname";
                this.ddl_group.DataValueField = "Id";
                this.ddl_group.DataBind();



                this.hid_sendurl.Value = this.Request.UrlReferrer.AbsoluteUri;
                string action = this.Request.QueryString["action"];
                string id = this.Request.QueryString["id"];
                this.hid_action.Value = action;
                this.hid_id.Value = id;
                if (action == ActionType.delete.ToString())
                {
                    Ecreate.Popedom.tSysuser user = userService.getById(id);
                    if (user.Group.Adminflag == 1)
                    {
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    userService.DeleteUser(user);
                    this.Response.Redirect(this.hid_sendurl.Value);
                }
                if (action == ActionType.edit.ToString())
                {
                    Ecreate.Popedom.tSysuser user = userService.getById(id);
                    if (user.Group.Adminflag == 1)
                    {
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">     alert('管理员组不允许修改!')　　; </script>");
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    this.ddl_group.SelectedValue = user.Group.Id.ToString();
                    this.txt_name.Text = user.Username;
                    this.txt_id.Text = user.Id.ToString();
                    this.txt_id.ReadOnly = true;
                    this.txt_id.ReadOnly = false;
                    this.txt_password.Text = user.Password;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Groupid = this.ddl_group.SelectedValue;
            Ecreate.Popedom.tGroup group = groupService.getById(long.Parse(Groupid));

            if (this.hid_action.Value == ActionType.add.ToString())
            {
                Ecreate.Popedom.tSysuser user = new Ecreate.Popedom.tSysuser();
                user.Id = this.txt_id.Text;
                user.Username = this.txt_name.Text;
                user.Group = group;
                user.Password = this.txt_password.Text;
                try
                {
                    userService.InsertUser(user);

                }
                catch
                {
                    Response.Write("<script type=\"text/javascript\" language=\"javascript\">     alert('用户ID重复!')　　; </script>");
                    return;
                }
            }
            if (this.hid_action.Value == ActionType.edit.ToString())
            {

                Ecreate.Popedom.tSysuser user = new Ecreate.Popedom.tSysuser();
                user.Id = this.hid_id.Value;
                user.Username = this.txt_name.Text;
                user.Group = group;
                user.Password = this.txt_password.Text;
                userService.UpdateUser(user);
            }
            this.Response.Redirect(this.hid_sendurl.Value);
        }
    }
}
